<?php
namespace Admin\Model;
use Think\Model;
class CartModel extends Model 
{
	protected $insertFields = array('goods_id','goods_attr_id','goods_number');
/*	protected $updateFields = array('id','goods_id','goods_attr_id','goods_number','member_id');*/
	protected $_validate = array(
		array('goods_id', 'require', '商品id不能为空！', 1),
		array('goods_number', 'chkGoodsNumber', '库存量不足！', 1,'callback'),

	);
	/************************************ 其他方法 ********************************************/
	public function chkGoodsNumber($goodsNumber){
	    $gaid = I('post.goods_attr_id');
        sort($gaid,SORT_NUMERIC);
        $gaid =(string)implode(',',$gaid);
        $gnModel = D('goods_number');
        $gn = $gnModel->field('goods_number')->where(array(
            'goods_id'=>I('post.goods_id'),
            'goods_attr_id'=>$gaid
        ))->find();
        return($gn['goods_number'] >= $goodsNumber);
    }
    public function cartList(){
        $memberId = session('m_id');
        if($memberId){
            $data = $this->where(array(
                'member_id'=>array('eq',$memberId),
            ))->select();
        }else{
            $_data =isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']):array();
            $data =array();
            foreach($_data as $k=> $v){
                $_k =explode('-',$k);
                $data[]=array(
                    'goods_id'=>$_k[0],
                    'goods_attr_id'=>$_k[1],
                    'goods_number'=>$v,
                );
            }
        }
        $gModel = D('Admin/Goods');
        $gaModel = D('goods_attr');
        foreach($data as $k=> &$v){
            $info = $gModel->field('goods_name,mid_logo')->find($v['goods_id']);
            $v['goods_name'] = $info['goods_name'];
            $v['mid_logo'] =$info['mid_logo'];
            $v['price'] =$gModel->getMemberPrice($v['goods_id']);
            if($v['goods_attr_id']){
                $v['gaData'] =$gaModel->alias('a')->field('a.attr_value,b.attr_name')
                    ->join('__ATTRIBUTE__ b ON a.attr_id=b.id')
                    ->where(array(
                        'a.id'=>array('in',$v['goods_attr_id'])
                    ))->select();
            }
        }
        return $data;
    }

    /*********************************/
    public function editGoodsNumber(){
        $memberId = session('m_id');
        if($memberId){
            foreach( I( 'post.' ) as $k => $v ){
                $k = explode( '-', $k );
                $this->where(array(
                    'member_id'=>array('eq',$memberId),
                    'goods_id' => $k[0],
                    'goods_attr_id' => $k[1]
                ))->save( array( 'goods_number' => $v ) );
            }
        }else{
            $data =isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']):array();
            foreach( I( 'post.' ) as $k => $v ){
                $data[$k] = $v;
            }
            setcookie( 'cart', serialize( $data ), time() + 86400, '/' );
        }
    }
    /*********************************/
    public function clear(){
        $this->where(array(
        'member_id'=>array('eq',session('m_id')),
        ))->delete();
    }
    public function clearCart($cartId){
        $_cartId = explode('-',$cartId);
        $goods_id = $_cartId[0];
        $godos_attr_id = $_cartId[1];
        $ret = array();
        if(session('m_id')){
            $this->where(array(
                'goods_id'=>array('eq',$goods_id),
                'goods_attr_id'=>array('eq',$godos_attr_id),
            ))->delete();
            $ret['msg'] ='删除成功!';
            $ret['code'] ='success';
        }else{
            $k = I('get.id');
            $data =isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']):array();
            unset($data[$k]);
            setcookie('cart',serialize($data),time() + 86400,'/');
            $ret['msg'] ='删除成功!';
            $ret['code'] ='success';
        }
        echo json_encode($ret);
    }
    public function add(){
        $memberId = session('m_id');
        sort($this->goods_attr_id,SORT_NUMERIC);
        $this->goods_attr_id =(string)implode(',',$this->goods_attr_id);
        if($memberId){
            $goodsNumber =$this->goods_number;
            $has = $this->field('id')->where(array(
                'goods_number'=>$goodsNumber,
                'member_id'=>$memberId,
                'goods_id' =>$this->goods_id,
                'goods_attr_id'=>$this->goods_attr_id
            ))->find();
            if($has){
                $this->where(array(
                    'id'=>array('eq',$has['id']),
                ))->setInc('goods_number',$goodsNumber);
            }else{
                parent::add(array(
                    'member_id'=>$memberId,
                    'goods_id' =>$this->goods_id,
                    'goods_attr_id'=>$this->goods_attr_id,
                    'goods_number'=> $this->goods_number
                ));
            }
        }else{
            $cart = isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']):array();
            $key =$this->goods_id . '-'.$this->goods_attr_id;
            if(isset($cart[$key])){
                $cart[$key] += $this->goods_number;
            }else{
                $cart[$key] =$this->goods_number;
            }
            setcookie('cart',serialize($cart),time()+30*86400,'/');
        }
        return TRUE;
}
public function moveDataToDb(){
    $memberId = session('m_id');
    if($memberId){
        $cart = isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']):array();
        foreach($cart as $k => $v){
            $_k = explode('-',$k);
            $has = $this->field('id')->where(array(
                'member_id'=>$memberId,
                'goods_id' =>$_k[0],
                'goods_attr_id'=>$_k[1]
            ))->find();
            if($has){
                $this->where(array(
                    'id'=>array('eq',$has['id']),
                ))->setInc('goods_number',$v);
            }else{
                parent::add(
                    array(
                        'member_id'=>$memberId,
                        'goods_id' =>$_k[0],
                        'goods_attr_id'=>$_k[1],
                        'goods_number'=>$v
                    )
                );
            }
        }
        setcookie('cart','',time()-1,'/');
    }
}
}